package com.fantasy.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.util.Log;

public class IMUtil {
	private static final String TAG = "IMUtil";
	public static final int IM_SOCKET_PORT = 22581;
	public static final String MULTICAST_ADDRESS = "255.255.255.255";

	public static final String LOGIN = "LOGIN";
	public static final String QUIT = "QUIT";
	public static final String MESSAGE = "MESSAGE";
	public static final String SPLIT = ":";
	public static final String FEEDBACK = "FEEDBACK";

	public static String getHostIPAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e(TAG, ex.toString());
		}
		return null;

	}

	public static String getUserName() {
		return null;
	}

	public static InetAddress getHostIP() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					return inetAddress;
				}
			}
		} catch (SocketException ex) {
			Log.e(TAG, ex.toString());
		}
		return null;

	}

	public static String sendLoginMessage() {
		StringBuilder sb = new StringBuilder();
		sb.append(LOGIN);
		sb.append(SPLIT);
		sb.append(MULTICAST_ADDRESS);
		sb.append(SPLIT);
		sb.append(getHostIPAddress());
		sb.append(SPLIT);

		return sb.toString();
	}

	public static String feedBackLogin(String targetIp) {
		StringBuilder sb = new StringBuilder();
		sb.append(FEEDBACK);
		sb.append(SPLIT);
		sb.append(targetIp);
		sb.append(SPLIT);
		sb.append(IMUtil.getHostIPAddress());
		sb.append(IMUtil.SPLIT);
		return sb.toString();
	}

	public static String sendMessage(String message) {
		StringBuilder sb = new StringBuilder();
		sb.append(MESSAGE);
		sb.append(SPLIT);
		sb.append(IMUtil.getHostIPAddress());
		sb.append(SPLIT);
		sb.append(message);
		sb.append(SPLIT);
		return sb.toString();
	}

	public static String sendQuitMsg() {
		StringBuilder sb = new StringBuilder();
		sb.append(QUIT);
		sb.append(SPLIT);
		sb.append(getHostIPAddress());
		sb.append(SPLIT);
		return sb.toString();

	}

	public static String sendMessage(String targetIp, String msg) {
		StringBuilder sb = new StringBuilder();
		sb.append(MESSAGE);
		sb.append(SPLIT);
		sb.append(targetIp);
		sb.append(SPLIT);
		sb.append(getHostIPAddress());
		sb.append(SPLIT);
		sb.append(msg);
		return sb.toString();
	}
}
